package com.travel.server.manager.impl.sal;

import com.travel.common.CommonConstant;
import com.travel.common.exception.WorkCode;
import com.travel.common.exception.WorkException;
import com.travel.common.util.CommonUtil;
import com.travel.common.util.DateUtil;
import com.travel.server.manager.dto.request.project.TimeRequest;
import com.travel.server.manager.impl.vo.DataDateVo;

import java.util.Calendar;
import java.util.Date;

/**
 * @Author: LiZhenhui
 * @Date: 2018/12/7 19:28
 * @Desc:
 */
public class OrderDataSal {

    public static DataDateVo getDate(Integer type, TimeRequest request){
        Date startTime = DateUtil.getDataTime(request.getStartTime());
        Date endTime = DateUtil.getDataEndTime(request.getEndTime());
        Date lastTime;
        if(type.equals(CommonConstant.DAY)){
            lastTime = DateUtil.getIndexDay(startTime, Calendar.DATE, -1);
        }else if(type.equals(CommonConstant.WEEK)){
            lastTime = DateUtil.getIndexDay(startTime, Calendar.DATE, -7);
        }else if(type.equals(CommonConstant.MONTH)){
            lastTime = DateUtil.getIndexDay(startTime, Calendar.MONTH, -1);
        }else if(type.equals(CommonConstant.DEFINE)){
            Date date2 = DateUtil.getDataTime(request.getEndTime());
            int days = (int) ((date2.getTime() - startTime.getTime()) / (1000*3600*24));
            lastTime = DateUtil.getIndexDay(startTime, Calendar.DATE, 0-days);
        }else if(type.equals(CommonConstant.QUARTER)){
            lastTime = DateUtil.getIndexDay(startTime, Calendar.MONTH, -3);
        }
        else{
            throw new WorkException(WorkCode.INVALID_PARAM);
        }
        DataDateVo vo  = new DataDateVo();
        vo.setStartTime(startTime);
        vo.setEndTime(endTime);
        vo.setLastTime(lastTime);
        return vo;
    }

    public static String getProportion(Integer current, Integer last){
        return CommonUtil.getProportion(current - last, last);
    }

    public static String getCompare(Integer type){
        String compare;
        if(type.equals(CommonConstant.DAY)){
            compare = "较前一天";
        }else if(type.equals(CommonConstant.WEEK)){
            compare = "较前一周";
        }else if(type.equals(CommonConstant.MONTH)){
            compare = "较前一月";
        }else if(type.equals(CommonConstant.DEFINE)){
            compare = "较前一期";
        }else if(type.equals(CommonConstant.QUARTER)){
            compare = "较前一季度";
        }
        else{
            throw new WorkException(WorkCode.INVALID_PARAM);
        }
        return compare;
    }

    public static String getHead(Integer type){
        String head;
        if(type.equals(CommonConstant.DAY)){
            head = "天";
        }else if(type.equals(CommonConstant.WEEK)){
            head = "周";
        }else if(type.equals(CommonConstant.MONTH)){
            head = "月";
        }else if(type.equals(CommonConstant.DEFINE)){
            head = "期";
        }else if(type.equals(CommonConstant.QUARTER)){
            head = "季度";
        }
        else{
            throw new WorkException(WorkCode.INVALID_PARAM);
        }
        return head;
    }
}
